import { JSBridge } from "../JSBridge";
import QuLe from "../QuLe";
import SDKManager from "../SDKManager";

export default class TouTiaoUtils {

    /** 列出其他游戏列表 */
    static get isIOS(): boolean {
        let tt = JSBridge.getTTObj();
        if (!tt) return false;
        const systemInfo = tt.getSystemInfoSync();
        return systemInfo.platform == 'ios';
    }

    /** 列出其他游戏列表 */
    static showOtherGamesDialog(appid: string, query?: any, extraData?: any) {
        let tt = JSBridge.getTTObj();
        if (!tt) return;
        console.log('尝试跳转到小游戏: ' + appid);
        const systemInfo = tt.getSystemInfoSync();
        // iOS 不支持，建议先检测再使用
        if (systemInfo.platform !== "ios") {
            tt.onNavigateToMiniProgram((res) => {
                console.log("tt.onNavigateToMiniProgram", JSON.stringify(res));
                SDKManager.instance.navigateToOtherGame("");
            });
            // 打开互跳弹窗
            tt.showMoreGamesModal({
                appLaunchOptions: [
                    {
                        appId: appid,
                        query: '?' + query,
                        extraData: extraData
                    }
                ],
                success(res) {
                    console.log("头条显示更多游戏成功", res.errMsg);
                },
                fail(res) {
                    console.error("头条显示更多游戏失败", res.errMsg);
                }
            });
        }
    }

    /** 监听游戏网络状态 */
    static listenNetworkStatusChange(callback: Function) {
        let tt = JSBridge.getTTObj();
        if (!tt) return;
        tt.onNetworkStatusChange((res) => {
            callback && callback(res.networkType != 'none');
        })
    }

}
